package dp.ua.epic.pages;

import epic.domain.Hero;
import epic.domain.PathDecision;
import epic.services.HeroService;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountMixedParam;

/**
 * Class that responcible for 'Chose' page
 * 
 * @author Chris
 * @date 26.01.2011 12:50:23
 */
@MountPath(path = "chosen")
@MountMixedParam(parameterNames = {Chosen.FATE_ID_PARAMETR_NAME})
public class Chosen extends BasePage {

    static final String FATE_ID_PARAMETR_NAME = "fateId";
    private static final String HERO_FATE_LABEL_ID = "heroFate";
    
    @SpringBean
    private HeroService heroService;

    public Chosen(final PageParameters parameters) {
        PathDecision pathDecision = getPathDecisionFromRequest(parameters);
        if (getEpicSession().isHeroWithoutFate() || heroHaveUndefinedFate()) {
            makeCurrentHeroDescision(pathDecision);
        }
        add(new BookmarkablePageLink("fatePageLink", Fate.class));
        add(new Label(HERO_FATE_LABEL_ID, new ResourceModel(heroDescition().toString())));
    }

    private PathDecision getPathDecisionFromRequest(PageParameters parameters) {
        final int defaultDescisionId = PathDecision.UNDEFINED.getDescisionId();
        int chosenPathId = parameters.getAsInteger(FATE_ID_PARAMETR_NAME, defaultDescisionId);
        return PathDecision.getDescisionById(chosenPathId);
    }

    private void makeCurrentHeroDescision(PathDecision pathDecision) {
        Hero hero = getEpicSession().getHero();
        heroService.setHeroPathDescision(hero, pathDecision);
    }

    private boolean heroHaveUndefinedFate() {
        return getEpicSession().getHero().getDecision() == PathDecision.UNDEFINED;
    }

    private PathDecision heroDescition() {
        return getEpicSession().getHero().getDecision();
    }
}
